package com.breakfast.fun.adapter.home;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.breakfast.fun.App;
import com.breakfast.fun.R;
import com.breakfast.fun.data.entity.home.ProductClasses;
import com.breakfast.fun.ui.home.product.FreeProductActivity;
import com.breakfast.fun.ui.home.product.NormalProductActivity;
import com.breakfast.fun.ui.home.product.SpecialProductActivity;
import com.breakfast.fun.util.BlurUtil;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.ScreenUtil;
import com.breakfast.fun.view.widget.RoundImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/12/19.
 */

public class HomeHeadIconsAdapter extends RecyclerView.Adapter<HomeHeadIconsAdapter.ViewHolder> {


    private LayoutInflater mLayoutInflater;
    private List<ProductClasses> mProductClasses;
    ViewHolder holder;
    private OnItemClickListener mOnItemClickListener;
    private Context mContext;

    //谷歌分析
    protected Tracker mTracker;

    public interface OnItemClickListener {
        void OnClickListener(View view, int position);
    }


    public HomeHeadIconsAdapter(List<ProductClasses> mProductClasses, Context mContext) {
        mLayoutInflater = LayoutInflater.from(mContext);
        mTracker = App.getApp().getDefaultTracker();
        this.mContext = mContext;
        this.mProductClasses = mProductClasses;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_home_head_icons, parent, false);
        holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.mLlItemHomeIcon.getLayoutParams();
        params.width = (int) ((ScreenUtil.getScreenWidth(mContext) - 10) / mProductClasses.size());
//        L.d(params.width + "~" + ScreenUtil.getScreenWidth(mContext));
        holder.mLlItemHomeIcon.setLayoutParams(params);


        holder.mTvItemHomeIcon.setText(mProductClasses.get(position).getClassName());

        Glide.with(mContext)
                .load(mProductClasses.get(position).getCategoryImage())
                .placeholder(R.drawable.bg_cache)
                .error(R.drawable.bg_cache)
                .crossFade()
                .into(holder.mIvItemHomeIcon);


        ProductClasses curProductClasses = getItem(position);

//        //4.5           一页显示4.5个
//        params.setMargins((ScreenUtil.getScreenWidth(mContext)-10)/10,10,(ScreenUtil.getScreenWidth(mContext)-10)/10,10);
//        holder.mLlItemHomeIcon.setLayoutParams(params);
//
//        params.width = (ScreenUtil.getScreenWidth(mContext))-20/6;

//        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.mLlItemHomeIcon.getLayoutParams();
//        params.width = 100;
//        L.d(params.width + "~" + ScreenUtil.getScreenWidth(mContext));
//        holder.mLlItemHomeIcon.setLayoutParams(params);

        holder.mLlItemHomeIcon.setOnClickListener(v -> {
            if (curProductClasses.getType() == 0) {
                //普通分类
                mTracker.send(new HitBuilders.EventBuilder()
                        .setCategory("Action")
                        .setAction("Android-点击普通分类-" + curProductClasses.getClassName())
                        .build());
                Intent intent = new Intent(mContext, NormalProductActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("product", mProductClasses.get(position));
                mContext.startActivity(intent);
            } else if (curProductClasses.getType() == 1) {
                //逼格类
                mTracker.send(new HitBuilders.EventBuilder()
                        .setCategory("Action")
                        .setAction("Android-点击逼格类-" + curProductClasses.getClassName())
                        .build());
                Intent intent = new Intent(mContext, SpecialProductActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("product", curProductClasses);
                mContext.startActivity(intent);
            } else if (curProductClasses.getType() == 2) {
                //优惠类
                mTracker.send(new HitBuilders.EventBuilder()
                        .setCategory("Action")
                        .setAction("Android-点击优惠类-" + curProductClasses.getClassName())
                        .build());
                Intent intent = new Intent(mContext, FreeProductActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("product", curProductClasses);
                mContext.startActivity(intent);
            }
        });

    }

    public void removeAll() {
        mProductClasses.clear();
    }

    @Override
    public int getItemCount() {
        return mProductClasses.size();
    }

    public ProductClasses getItem(int position) {
        return mProductClasses.get(position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.riv_item_home_icon)
        RoundImageView mRivItemHomeIcon;
        @Bind(R.id.tv_item_home_icon_tips)
        TextView mTvItemHomeIconTips;
        @Bind(R.id.iv_item_home_icon)
        ImageView mIvItemHomeIcon;
        @Bind(R.id.tv_item_home_icon)
        TextView mTvItemHomeIcon;
        @Bind(R.id.ll_item_home_icon)
        LinearLayout mLlItemHomeIcon;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
